﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqData;
using CDB.LinqModel.Data;
using CDB.LinqModel;

public partial class back_ViewArchitects : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Architect architect;
    List<Architect> architects = new List<Architect>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindArchitects();
        }
    }

    private void bindArchitects()
    {
        architect = new Architect(_dataObject);
        architects = architect.GetAllArchitects();

        if (architects.Count > 0)
        {
            grdArchitect.Visible = true;
            grdArchitect.DataSource = architects;
            grdArchitect.DataBind();
        }
        else
        {
            grdArchitect.Visible = false;
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdArchitect_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        grdArchitect.PageIndex = e.NewPageIndex;
        bindArchitects();
    }
    protected void grdArchitect_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower() == "editarchitect")
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }
        if (e.CommandName.ToString().ToLower().Trim().Equals("deletearchitect"))
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnArchitectId = (System.Web.UI.WebControls.Label)row.FindControl("hdnArchitectId");
            architect = new Architect(_dataObject);
            architect.Id = Convert.ToInt32(hdnArchitectId.Text);
            architect.Delete();
            bindArchitects();
            divRed.InnerHtml = "<strong>Deletion!</strong>&nbsp;Record Deleted Successfully!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
        if (e.CommandName.ToString().ToLower() == "architecturalwork")
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnArchitecturalWorks = (System.Web.UI.WebControls.Button)row.FindControl("btnArchitecturalWorks");
            Response.Redirect(btnArchitecturalWorks.PostBackUrl);
        }
    }
    protected void grdArchitect_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label hdnArchitectId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnArchitectId");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnEdit");
            System.Web.UI.WebControls.Button btnArchitecturalWorks = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnArchitecturalWorks");
            //Label lblDOB = (Label)e.Row.FindControl("lblDOB");
            //lblDOB.Text = Convert.ToDateTime(lblDOB.Text).ToShortDateString();
            btnEdit.PostBackUrl = "AddArchitects.aspx?Id=" + hdnArchitectId.Text;
            btnArchitecturalWorks.PostBackUrl = "architecturalWork.aspx?Id=" + hdnArchitectId.Text;
        }
    }
}